package cn.cqnu.service.impl;

import cn.cqnu.domin.dto.AdminMatchListDto;
import cn.cqnu.domin.dto.FrontMatchDto;
import cn.cqnu.domin.entity.Match;
import cn.cqnu.domin.vo.ResponseResult;
import cn.cqnu.domin.vo.MatchListVo;
import cn.cqnu.domin.vo.frontMatchListVo;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.mapper.MatchMapper;
import cn.cqnu.service.MatchService;
import cn.cqnu.utils.BeanCopyUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * (Match)表服务实现类
 *
 * @author makejava
 * @since 2024-01-19 20:38:44
 */
@Service
public class MatchServiceImpl extends ServiceImpl<MatchMapper, Match> implements MatchService {
    @Autowired
    MatchMapper matchMapper;

    @Override
    public void saveImg(String matchId, String backImgUrl) {
        //将图片路径保存到对应的比赛信息中
        if (matchId == null)
            return;
        UpdateWrapper<Match> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("match_id", matchId).setSql("match_backImg = '" + backImgUrl + "'");
        update(updateWrapper);

    }

    @Override
    public HashMap<String, Object> adminMatchList(AdminMatchListDto matchInfo) {
        Match match = BeanCopyUtil.copyBean(matchInfo, Match.class);

        QueryWrapper<Match> queryWrapper = Wrappers.<Match>query();
        if (matchInfo.getMatchId() != null && matchInfo.getMatchId() != "") {
            queryWrapper.like("match_id", matchInfo.getMatchId());
        }
        if (matchInfo.getMatchName() != null && matchInfo.getMatchName() != "") {
            queryWrapper.like("match_name", matchInfo.getMatchName());
        }
        if (matchInfo.getOrganier() != null && matchInfo.getOrganier() != "") {
            queryWrapper.like("organier", matchInfo.getOrganier());
        }
        if (matchInfo.getMatchDirector() != null && matchInfo.getMatchDirector() != "") {
            queryWrapper.like("match_director", matchInfo.getMatchDirector());
        }
        if (matchInfo.getType() != null) {
            queryWrapper.eq("type", matchInfo.getType());
        }
        if (matchInfo.getStartTime() != null)
            queryWrapper.lt("start_time", matchInfo.getStartTime());

        Page page = new Page(matchInfo.getPageNum(), matchInfo.getPageSize());
        Page<Match> page1 = page(page, queryWrapper);
        List<Match> records = page1.getRecords();
        List<MatchListVo> MatchListVos = BeanCopyUtil.copyList(records, MatchListVo.class);

        HashMap<String, Object> result = new HashMap<>();
        result.put("data", MatchListVos);
        result.put("total", page1.getTotal());

        return result;
    }

    @Override
    public List<frontMatchListVo> frontMatchList(FrontMatchDto frontMatchDto) {
        QueryWrapper<Match> wrapper = new QueryWrapper<>();
        if (frontMatchDto.getName() != null)
            wrapper.like("match_name", frontMatchDto.getName());
        if (frontMatchDto.getType() != null)
            wrapper.eq("type", frontMatchDto.getType());
        List<Match> list = list();
        List<frontMatchListVo> frontMatchListVos = BeanCopyUtil.copyList(list, frontMatchListVo.class);
        return frontMatchListVos;
    }

    @Override
    public ResponseResult addMatch(Match matchInfo) {
        Match match = getOne(new QueryWrapper<Match>().eq("match_id", matchInfo.getMatchId()));
        if (match != null) {
            return ResponseResult.error(SystemReturnEnum.MATCH_EXISTED);
        }
        save(matchInfo);
        return ResponseResult.ok();
    }

    @Override
    public void updateMatch(Match matchInfo) {
        update(matchInfo, new LambdaQueryWrapper<Match>().eq(Match::getMatchId, matchInfo.getMatchId()));
    }

    @Override
    public void deleteMatch(String id) {
        remove(new QueryWrapper<Match>().eq("match_id", id));
    }

    @Override
    public MatchListVo getMatchById(String id) {
        Match matchInfo = getOne(new QueryWrapper<Match>().eq("match_id", id));
        MatchListVo matchListVo = BeanCopyUtil.copyBean(matchInfo, MatchListVo.class);
        return matchListVo;
    }

}
